﻿using NDK.Module.BLL;
using NDK.Motion.BLL;
using NDK.Motion.MachineManager;
using NDK.Motion.MachineResources;
using NDK.Motion.Monitor;
using NDK.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
using static NDK.Module.Model.Common;

namespace NDK.Module.StationLogic
{
    [Monitor(Description = "搬运平台工位")]
    public class CarryPlatform : StationLogicBase
    {
        #region 通用
        //记录当前步骤，用于步骤变化的比较
        private StationStep _curStep = StationStep.WaitMacInit;
        //对外提供工位的步骤变化事件
        public override event PropertyChangedEventHandler PropertyChanged;
        //对外提供唯一实例
        public static CarryPlatform Instance = new CarryPlatform();
        //私有化的构造函数
        private CarryPlatform() : base()
        {
            Name = "搬运平台";
        }
        //步骤字段、属性
        private StationStep _step = StationStep.WaitMacInit;
        [Monitor(Description = "当前步骤")]
        public StationStep Step
        {
            get
            {
                return _step;
            }
            set
            {
                if (_curStep != value)
                {
                    _step = value;
                    _curStep = value;
                    FileUtils.WriteLogToTxt($"{Step.ToString()}  CT:{Math.Round((double)swStepTime.ElapsedMilliseconds / 1000, 3)}s", logFold, logBaseInfo, isWriteLog);
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(value.ToString()));
                    swStepTime.Restart();
                }
            }
        }
        #endregion

        #region 变量
        public bool 告知PPG接料完成 { set; get; }
        public bool 告知下料允许接料 { set; get; }

        #endregion

        /// <summary>
        /// 工站初始化
        /// </summary>
        public override void Init()
        {
            base.Init();
            Step = StationStep.WaitMacInit;
            logBaseInfo = "搬运平台";

            ListVacuum = new List<OutputSetting>() { Machine.DicOutputIO["下料真空吸"] };
            ListCylinder = new List<ICylinder>() { Machine.DicDriveCylinder["下料气缸"] };
            SNCode1 = string.Empty;
            LocalGRRCount = 0;
            告知PPG接料完成 = false;
            告知下料允许接料 = false;
        }

        /// <summary>
        /// 工站动作流程
        /// </summary>
        /// <param name="mode"></param>
        public override void ActionProcess()
        {
            try
            {
                switch (Step)
                {
                    case StationStep.WaitMacInit:
                        Step = StationStep.Start;
                        break;
                    case StationStep.Start:
                        if (PublicMethod.OffVacuum(ListVacuum) && PublicMethod.HomeCylinder(ListCylinder) && MotionControl.AxesMove(Machine.DicPos[Name]["接料等待位"]))
                        {
                            Step = StationStep.等待PPG允许到接料位;
                        }
                        break;
                    case StationStep.等待PPG允许到接料位:
                        if (PPG.Instance.告知搬运平台允许到接料位)
                        {
                            if (Math.Abs(Machine.DicPos["中转平台"]["接料位"].AxisList.Find(X => X.Name == "中转平台X轴").GetPos() - Machine.DicPos["中转平台"]["出料位"].GetPos("中转平台X轴")) > 100)
                            {
                                if (MotionControl.AxesMove(Machine.DicPos[Name]["接料位"]))
                                {
                                    PPG.Instance.告知搬运平台允许到接料位 = false;
                                    Step = StationStep.等待PPG允许接料;
                                }
                            }
                        }
                        break;
                    case StationStep.等待PPG允许接料:
                        if (PPG.Instance.告知搬运平台允许接料)
                        {
                            if (MotionControl.AxesMove(Machine.DicPos[Name]["接料位"]) && PublicMethod.WorkCylinder(ListCylinder) &&
                            PublicMethod.OffVacuum(PPG.Instance.ListVacuum, false) && PublicMethod.OpenVacuum(ListVacuum))
                            {
                                SNCode1 = PPG.Instance.SNCode1;
                                PPG.Instance.告知搬运平台允许接料 = false;
                                Step = StationStep.气缸上升;
                            }
                        }
                        break;
                    case StationStep.气缸上升:
                        if (PublicMethod.HomeCylinder(ListCylinder))
                        {
                            告知PPG接料完成 = true;
                            Step = StationStep.移动到出料位;
                        }
                        break;
                    case StationStep.移动到出料位:
                        if (MotionControl.AxesMove(Machine.DicPos[Name]["出料位"]))
                        {
                            告知下料允许接料 = true;
                            Step = StationStep.等待下料平台接料完成;
                        }
                        break;
                    case StationStep.等待下料平台接料完成:
                        if (BlankingPlatform.Instance.告知搬运平台接料完成)
                        {
                            PublicMethod.OffVacuum(ListVacuum);
                            BlankingPlatform.Instance.告知搬运平台接料完成 = false;
                            Step = StationStep.End;
                        }
                        break;
                    case StationStep.End:
                        Step = StationStep.WaitMacInit;
                        break;
                }
            }
            catch (Exception ex)
            {
                UILog_Error($"[ {logBaseInfo} ActionProcess Thread ERROR] {ex.Message}");
                if (AlarmPause(logBaseInfo, $"当前状态:{Step} 报警异常:{ex.Message}", "点击确定，再试一次；点击取消，继续运行", "再试一次", "继续运行") == DialogResult.OK)
                {

                }
            }
        }

        public override void EmptyActionProcess()
        {
            ActionProcess();
        }

        public override void GRRActionProcess()
        {
            ActionProcess();
        }

        /// <summary>
        /// 工位步骤定义
        /// </summary>
        public enum StationStep
        {
            /// <summary>
            /// 等待机台初始化
            /// </summary>
            [Description("等待机台初始化")]
            WaitMacInit,
            [Description("开始")]
            Start,
            等待PPG允许到接料位,
            等待PPG允许接料,
            气缸上升,
            移动到出料位,
            等待下料平台接料完成,
            [Description("工站完成")]
            End,
        }
    }
}
